package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.AdminSmResContr;
import cn.com.yusys.yusp.admin.service.AdminSmResContrService;
import cn.com.yusys.yusp.admin.service.impl.UserServiceImpl;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminContrPointResource
 * @类描述: 控制点管理接口层
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-21 17:57
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 *        -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("/api/adminsmrescontr")
public class AdminSmResContrResource extends CommonResource<AdminSmResContr, String> {

    @Autowired
    private AdminSmResContrService adminSmResContrService;
    @Autowired
    private UserServiceImpl userCache;

    @Override
    protected CommonService getCommonService() {
        return this.adminSmResContrService;
    }

    /**
     * @方法名称:getContrInfo
     * @方法描述:查询控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/getcontrinfo")
    public ResultDto<List<Map<String, Object>>> getContrInfo(QueryModel queryModel) {
        List<Map<String, Object>> list = adminSmResContrService.getContrInfo(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * @方法名称: ifCodeReapt
     * @方法描述: 保存控制点信息前，判断业务功能是否已关联相同控制操作代码的控制点
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/ifcoderepeat")
    public ResultDto<List<Map<String, Object>>> ifCodeReapt(@RequestParam(required = false) String funcId,
            @RequestParam(required = false) String contrCode, @RequestParam(required = false) String contrId) {
        List<Map<String, Object>> list = adminSmResContrService.ifCodeRepeat(funcId, contrCode, contrId);
        return new ResultDto<List<Map<String, Object>>>(list);
    }

    /**
     * @方法名称: createContr
     * @方法描述: 新增控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/createcontr")
    public ResultDto<Integer> createContr(@RequestBody AdminSmResContr adminSmResContr) {
        int result = this.adminSmResContrService.createContr(adminSmResContr);
        ResultDto<Integer> resultDto = new ResultDto<>();
        if(result >= 1){
        	//删除缓存
        	userCache.cleanAllContrUrl();
        	//刷新控制点缓存
        	userCache.findAllContrUrl(null);
            resultDto.setMessage("新增控制点信息" + adminSmResContr.getContrName());
        }
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * @方法名称: editContr
     * @方法描述: 修改控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/editcontr")
    public ResultDto<Integer> editContr(@RequestBody AdminSmResContr adminSmResContr) {
        int result = this.adminSmResContrService.editContr(adminSmResContr);
        ResultDto<Integer> resultDto = new ResultDto<>();
        if(result >= 1){
        	//删除缓存
        	userCache.cleanAllContrUrl();
        	//刷新控制点缓存
        	userCache.findAllContrUrl(null);
            resultDto.setMessage("修改控制点信息");
        }
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * @方法名称: deleteContr
     * @方法描述: 删除控制点
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/deletecontr/{ids}")
    protected ResultDto<Integer> deletecontr( @PathVariable String ids) {
        int result = adminSmResContrService.deleteContr(ids);
        ResultDto<Integer> resultDto = new ResultDto<>();
        if(result >= 1){
        	//删除缓存
        	userCache.cleanAllContrUrl();
        	//刷新控制点缓存
        	userCache.findAllContrUrl(null);
            resultDto.setMessage("删除控制点id "+ids);
        }
        resultDto.setData(result);
        return resultDto;
    }

    /**
     * @方法名称: getFuncTree
     * @方法描述: 左侧业务功能树查询
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/treequery")
    public ResultDto<List<Map<String, Object>>> getFuncTree(QueryModel queryModel) {
        List<Map<String, Object>> list = adminSmResContrService.getFuncTree(queryModel);
        return new ResultDto<List<Map<String, Object>>>(list.size(),list);
    }

}
